﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StructureDiagram
{
    public class DiagramElement
    {
        List<DiagramElement> previousElementList;
        List<DiagramElement> nextElementList;

        //

        public DiagramElement()
        {
            previousElementList = new List<DiagramElement>();
            nextElementList = new List<DiagramElement>();
        }

        /// <summary>
        /// Getting previous elements of the diagram element
        /// </summary>
        /// <returns>Returns previous elements list</returns>
        public List<DiagramElement> GetPreviousElements()
        {
            return previousElementList;
        }

        /// <summary>
        /// Getting next elements of the diagram element
        /// </summary>
        /// <returns>Returns next elements list</returns>
        public List<DiagramElement> GetNextElements()
        {
            return nextElementList;
        }

        /// <summary>
        /// Checking for previous elements
        /// </summary>
        /// <param name="skipLooped">
        /// If this parameter is true, method returns false only if element 
        /// hasn't got any previous elements
        /// </param>
        /// <returns>Returns true if elemnt hasn't got previous elements</returns>
        public bool HasPreviousElements(bool skipLooped)
        {
            if (previousElementList.Count == 0)
                return false;

            if (skipLooped)
                return true;

            foreach (DiagramElement element in previousElementList)
            {
                if (element != this)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Checking for next elements
        /// </summary>
        /// <param name="skipLooped">
        /// If this parameter is true, method returns false only if element 
        /// hasn't got any next elements
        /// </param>
        /// <returns>Returns true if elemnt hasn't got previous elements</returns>
        public bool HasNextElements(bool skipLooped)
        {
            if (nextElementList.Count == 0)
                return false;

            if (skipLooped)
                return true;

            foreach (DiagramElement element in nextElementList)
            {
                if (element != this)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Connecting this diagram element with some previous diagram element
        /// </summary>
        /// <param name="element">Previous diagram element</param>
        public void ConnectWithPreviousElement(DiagramElement element)
        {
            if(!previousElementList.Contains(element))
                previousElementList.Add(element);
        }

        /// <summary>
        /// Connecting this diagram element with some next diagram element
        /// </summary>
        /// <param name="element">Next diagram element</param>
        public void ConnectWithNextElement(DiagramElement element)
        {
            if(!nextElementList.Contains(element))
                nextElementList.Add(element);
        }
    }
}
